package com.hc.smsbackup.sms;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;

public class SMSReader {

	private final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
	private final Uri SMS_SENT = Uri.parse("content://sms/sent");
	private Activity app;

	public SMSReader() {

	}

	public SMSReader(Activity app) {

		this.app = app;
	}

	/**
	 * Get all SMS in inbox
	 * 
	 * @return List SMS object
	 */
	public List<SMSData> getSMSInbox() {

		return getSMS(SMS_INBOX);
	}

	/**
	 * get All SMS in sent
	 * @return
	 */
	public List<SMSData> getSMSSent() {
		
		return getSMS(SMS_SENT);
	}

	/**
	 * Get all sms
	 * 
	 * @return List SMS object
	 */
	private List<SMSData> getSMS(Uri uri) {

		List<SMSData> smsData = new ArrayList<SMSData>();

		Cursor c = null;

		try {

			c = app.getApplicationContext().getContentResolver()
					.query(uri, null, null, null, null);

		} catch (Exception e) {

		}

		try {

			for (boolean hasData = c.moveToFirst(); hasData; hasData = c
					.moveToNext()) {

				SMSData sms = new SMSData();
				sms.set_id(c.getString(0));
				sms.setThread_id(c.getString(1));
				sms.setAddress(c.getString(2));
				sms.setPerson(c.getString(3));
				sms.setDate(c.getString(4));
				sms.setProtocol(c.getString(5));
				sms.setRead(c.getString(6));
				sms.setStatus(c.getString(7));
				sms.setType(c.getString(8));
				sms.setReply_path_present(c.getString(9));
				sms.setSubject(c.getString(10));
				sms.setBody(c.getString(11));
				sms.setService_center(c.getString(12));
				sms.setLocked(c.getString(13));
				sms.setError_code(c.getString(14));

				smsData.add(sms);
			}

		} catch (Exception e) {

		}

		return smsData;
	}
}
